'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //渲染
  async redian() {
    const { ctx, app } = this
    const data = await app.mysql.select('热点')

    ctx.body = {
      code: 200,
      data
    }
  }

  //药品
  async drugList() {
    const { ctx, app } = this
    const data = await app.mysql.select('商城')

    ctx.body = {
      code: 200,
      data
    }
  }

  //删除
  async del() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('热点', { id })
    ctx.body = {
      code: 200,
      data
    }
  }

  //药品删除
  async drugDel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('商城', { id })
    ctx.body = {
      code: 200,
      data
    }
  }

  //搜索
  async search() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 热点 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //药品搜索
  async drugSearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 商城 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //添加
  async rdadd() {
    const { ctx, app } = this
    const { oname, okeshi } = ctx.request.body;

    await app.mysql.insert('热点', { name: oname, title: okeshi, img: '1', tag: '1', read: '1', time: '1', praise: '1', key: Math.random().toString().slice(4, 6) })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }

  //药品添加
  async drugadd() {
    const { ctx, app } = this
    const { oname, okeshi } = ctx.request.body;

    await app.mysql.insert('商城', { title: oname, price: okeshi, info: '1', key: Math.random().toString().slice(4, 6), img: '//c1.yaofangwang.net/19/2808/f906e9ae4e1a25c72ad204fbb36367b4.jpg_syp.jpg', num: '1', tag: '处方药' })
    ctx.body = {
      code: 200,
      msg: '添加成功'
    }
  }

  //编辑
  async rdset() {
    const { ctx, app } = this;
    const { oname, okeshi, oid } = ctx.request.body
    await app.mysql.update('热点', { name: oname, title: okeshi, id: oid })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }

  //编辑
  async drugset() {
    const { ctx, app } = this;
    const { oname, okeshi, oid } = ctx.request.body
    await app.mysql.update('商城', { title: oname, price: okeshi, id: oid })
    ctx.body = {
      code: 200,
      msg: '编辑成功'
    }
  }
}

module.exports = HomeController;
